package cn.com.dpc.service;

import java.io.*;
import java.net.*;

import android.util.Log;

public class SocketClient {
	public static Socket client;

	public SocketClient(String site, int port) {
		try {
			client = new Socket(site, port);
			System.out.println("Client is created! site:" + site + " port:"
					+ port);
			new Thread() {
				public void run() {
					try {
						BufferedReader in = new BufferedReader(
								new InputStreamReader(client.getInputStream()),
								8192);
						while (true) {
							Log.v("SocketClient", in.readLine());
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				};
			}.start();

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String sendMsg(String msg) {
		try {
			PrintWriter out = new PrintWriter(client.getOutputStream());
			out.println(msg);
			out.flush();
			return "";
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}

	public void closeSocket() {
		try {
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		SocketClient sc = new SocketClient("218.249.218.105", 8888);
		sc.sendMsg("92");
		sc.sendMsg("95#111111");
		sc.sendMsg("92");
		sc.sendMsg("95#888888");
		sc.sendMsg("92");
		sc.sendMsg("95#111111");
	}

}